module RadianceInstrumentConfig

"""Collection of per-instrument channel metadata derived from legacy GSI configs."""
struct InstrumentSettings
    description::String
    channels::Vector{Int32}
    error_std::Vector{Float64}
    use_flags::Vector{Int8}
    tb_min::Vector{Float64}
    tb_max::Vector{Float64}
    min_valid_fraction::Float64
end

const AMSUA_CHANNELS = Int32.(1:15)
const AMSUA_ERROR_STD = [3.0, 2.2, 2.0, 0.6, 0.3, 0.23, 0.25, 0.275, 0.34, 0.4, 0.6, 1.0, 1.5, 2.0, 3.5]
const AMSUA_USE_FLAGS = Int8[1, 1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1]
const AMSUA_TB_MIN = [160.0, 150.0, 210.0, 240.0, 225.0, 210.0, 200.0, 190.0, 190.0, 195.0,
                      200.0, 210.0, 220.0, 230.0, 200.0]
const AMSUA_TB_MAX = [295.0, 285.0, 275.0, 275.0, 270.0, 260.0, 250.0, 240.0, 235.0, 230.0,
                      230.0, 240.0, 255.0, 265.0, 290.0]

const MHS_CHANNELS = Int32.(1:5)
const MHS_ERROR_STD = [2.5, 2.5, 2.0, 2.0, 3.0]
const MHS_USE_FLAGS = Int8[1, 1, 1, 1, 1]
const MHS_TB_MIN = [150.0, 150.0, 160.0, 170.0, 170.0]
const MHS_TB_MAX = [320.0, 320.0, 310.0, 300.0, 300.0]

const ATMS_CHANNELS = Int32.(1:22)
const ATMS_ERROR_STD = [4.5, 4.5, 4.5, 2.5, 0.55, 0.3, 0.3, 0.4, 0.4, 0.4, 0.45,
                        0.45, 0.55, 0.8, 3.0, 4.0, 4.0, 3.5, 3.0, 3.0, 3.0, 3.0]
const ATMS_USE_FLAGS = Int8[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1]
const ATMS_TB_MIN = [150.0, 150.0, 200.0, 230.0, 210.0, 200.0, 195.0, 190.0, 185.0, 185.0, 185.0,
                     185.0, 185.0, 190.0, 200.0, 200.0, 200.0, 200.0, 200.0, 200.0, 200.0, 200.0]
const ATMS_TB_MAX = [320.0, 320.0, 290.0, 280.0, 270.0, 260.0, 250.0, 240.0, 235.0, 235.0, 235.0,
                     235.0, 235.0, 240.0, 290.0, 300.0, 300.0, 295.0, 290.0, 285.0, 280.0, 275.0]

const IASI_CHANNELS = Int32[16, 38, 49, 51, 55, 57, 59, 61]
const IASI_ERROR_STD = [1.38, 0.72, 0.65, 0.65, 0.64, 0.65, 0.67, 0.62]
const IASI_USE_FLAGS = Int8[fill(1, length(IASI_CHANNELS))...]
const IASI_TB_MIN = fill(170.0, length(IASI_CHANNELS))
const IASI_TB_MAX = fill(320.0, length(IASI_CHANNELS))

const INSTRUMENT_SETTINGS = Dict(
    "amsua" => InstrumentSettings(
        "Advanced Microwave Sounding Unit-A",
        AMSUA_CHANNELS,
        AMSUA_ERROR_STD,
        AMSUA_USE_FLAGS,
        AMSUA_TB_MIN,
        AMSUA_TB_MAX,
        0.4
    ),
    "mhs" => InstrumentSettings(
        "Microwave Humidity Sounder",
        MHS_CHANNELS,
        MHS_ERROR_STD,
        MHS_USE_FLAGS,
        MHS_TB_MIN,
        MHS_TB_MAX,
        0.5
    ),
    "atms" => InstrumentSettings(
        "Advanced Technology Microwave Sounder",
        ATMS_CHANNELS,
        ATMS_ERROR_STD,
        ATMS_USE_FLAGS,
        ATMS_TB_MIN,
        ATMS_TB_MAX,
        0.35
    ),
    "iasi" => InstrumentSettings(
        "Infrared Atmospheric Sounding Interferometer",
        IASI_CHANNELS,
        IASI_ERROR_STD,
        IASI_USE_FLAGS,
        IASI_TB_MIN,
        IASI_TB_MAX,
        0.45
    )
)

"""Return instrument settings (channels, errors, use-flags, description)."""
function instrument_info(name::AbstractString)
    key = lowercase(strip(name))
    return get(INSTRUMENT_SETTINGS, key) do
        error("Instrument not registered: $name")
    end
end

end
